package 多线程07_线程池;

import java.util.concurrent.*;

/**
 * Java 单线程和线程池耗时的对比
 * @ClassName: TestThread01
 * @Author: AllenSun
 * @Date: 2022/3/18 上午1:46
 */
public class TestThread05 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        ExecutorService executorService2 = Executors.newFixedThreadPool(10);
        ExecutorService executorService3 = Executors.newSingleThreadExecutor();
        // Long startTime = System.currentTimeMillis();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                10,
                20,
                0L,
                TimeUnit.MICROSECONDS,
                new ArrayBlockingQueue<Runnable>(10));
        for (int i = 0; i < 100; i++) {
            // executorService1.execute(new MyTask(i));
            // executorService2.execute(new MyTask(i));
            // executorService3.execute(new MyTask(i));
            threadPoolExecutor.execute(new MyTask(i));
        }
        // System.out.println("时间："+(System.currentTimeMillis()-startTime));
    }
}

class MyTask implements Runnable {

    int i = 0;

    public MyTask(int i) {
        this.i=i;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"--"+i);
        try {
            //模拟业务逻辑
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
